誰說我們不能改變世界? 因著鐵人文,你也推動了台灣IT技術前進再前進。
哈囉大家好今天我一樣要來示範ListView的其他用法,今天會示範比較複雜一點的BaseAdapter,廢話不多說,那我就開始示範吧!
一開始一樣在activity_main.xml裡面加了一個ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
然後和前一天一樣建立個自定義的Item格式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding ="10dp"
>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/image"/>
<TextView
android:id="@+id/continent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35sp"
/>
</LinearLayout>
</LinearLayout>
我們建立一個ListAdapter然後繼承BaseAdapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import java.util.Map;
public class ListAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
List<Map<String,Object>> listItem;
public ListAdapter(Context context, List<Map<String, Object>> listItem) {
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.listItem = listItem;
}
@Override
public int getCount() {
return listItem.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = layoutInflater.inflate(R.layout.listitem,parent,false);
ImageView imageView = view.findViewById(R.id.image);
TextView textView = view.findViewById(R.id.continent);
imageView.setImageResource(Integer.valueOf(listItem.get(position).get("continent_image").toString()));
textView.setText(listItem.get(position).get("continent").toString());
return view;
}
}
最後上個MainActivity的程式碼
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private ListAdapter mListAdapter;
List<Map<String,Object>> areas = new ArrayList<Map<String,Object>>();
String [] continent = {"北美洲","南美洲","亞洲","歐洲","非洲","大洋洲","南極洲"};
int[] continent_image = {R.drawable.north_america,R.drawable.south_america,R.drawable.asia,R.drawable.europe
,R.drawable.africa,R.drawable.oceania,R.drawable.antarctica
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
for (int i = 0; i< continent_image.length; i++){
Map<String,Object> area = new HashMap<String, Object>();
area.put("continent_image", continent_image[i]);
area.put("continent",continent[i]);
areas.add(area);
}
mListAdapter = new ListAdapter(MainActivity.this,areas);
listView.setAdapter(mListAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,"您點選的是:"+ continent[position],Toast.LENGTH_SHORT).show();
}
});
}
}
今天的程式碼和昨天的差不多,布一樣的地方就是加了一個ListAdapter,當我們要繼承BaseAdapter時,我們要額外import一些class,他們的功能分別是:
那完成後的圖片和下圖類似
那今天的示範就到此為止了,謝謝大家的觀看。